package com.riku.security.controller;

import com.riku.security.common.SeCommonResponse;
import com.riku.security.common.SeSecurityException;
import com.riku.security.core.SeExternalUserService;
import com.riku.security.model.SeServiceUser;
import com.riku.security.model.SeUserParam;
import com.riku.security.service.SeServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1 on 2021/5/18.
 */
@Api(value = "security-service",description = "权限-服务")
@RestController
@RequestMapping("/se/service/")
public class SeServiceController {

    @Autowired
    private SeServiceService seServiceService;

    @Autowired
    private SeExternalUserService seExternalUserService;

    @ApiOperation("新增服务")
    @PostMapping("insertService")
    public SeCommonResponse insertService(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String serviceName
    ) {
        SeUserParam user = seExternalUserService.getNowUser();
        try {
            seServiceService.insertService(serviceCode, serviceName, user.getUserId(),user.getUserName());
        } catch (SeSecurityException e) {
            return SeCommonResponse.fromException(e);
        }

        return SeCommonResponse.success();
    }

    @ApiOperation("删除服务")
    @DeleteMapping("deleteService")
    public SeCommonResponse deleteService(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seServiceService.deleteService(id);
        return SeCommonResponse.success();
    }

    @ApiOperation("查询服务授权的所有人员")
    @GetMapping("selectServiceUser")
    public SeCommonResponse<List<SeServiceUser>> selectServiceUser(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = false) @RequestParam(required = false) String userName
    ) {
        return SeCommonResponse.success(seServiceService.selectServiceUser(serviceCode,userName));
    }

    @ApiOperation("新增服务授权")
    @PostMapping("insertServiceUser")
    public SeCommonResponse insertServiceUser(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId,
            @ApiParam(required = true) @RequestParam(required = true) String userName
    ) {
        try {
            seServiceService.insertServiceUser(serviceCode,userId,userName);
        } catch (SeSecurityException e) {
            return SeCommonResponse.fromException(e);
        }

        return SeCommonResponse.success();
    }

    @ApiOperation("删除服务权限")
    @DeleteMapping("deleteServiceUser")
    public SeCommonResponse deleteServiceUser(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seServiceService.deleteServiceUser(id);
        return SeCommonResponse.success();
    }
}
